using System;
using System.Collections.Generic;
using LuaInterface;
using UnityEngine;
using UnityEngine.Rendering;

public class EffectOrderContainer : MonoBehaviour
{
    [NoToLua]
    public List<EffectRenderObj> renderList = new List<EffectRenderObj>();
    private int baseOrder = -1;

    [NoToLua]
    public int BaseOrder
    {
        get { return baseOrder; }
    }

    void OnDestroy()
    {
        renderList = null;
    }

    public void SetBaseOrder(int order)
    {
        if (baseOrder != order)
        {
            baseOrder = order;
            ChangeOrder();
        }
    }

    private void ChangeOrder()
    {
        for (int i = 0; i < renderList.Count; ++i)
        {
            var effectRenderObj = renderList[i];
            //特效同事做特效的时候，是以0为基准的
            if (effectRenderObj != null && !effectRenderObj.Equals(null))
            {
                if (effectRenderObj.renderer != null)
                {
                    effectRenderObj.renderer.sortingOrder = baseOrder + effectRenderObj.order;
                }
                if (effectRenderObj.sortingGroup != null)
                {
	                effectRenderObj.sortingGroup.sortingOrder = baseOrder + effectRenderObj.order;
                }
            }
        }
    }
}

[Serializable]
[NoToLua]
public class EffectRenderObj
{
    public Renderer renderer;
    public SortingGroup sortingGroup;
    public int order;
}